/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import Connection.SQLConnection;
import Entities.HOADON;
import Entities.NHANVIEN;
import Entities.SACH;
import eu.schudt.javafx.controls.calendar.DatePicker;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javax.swing.JOptionPane;


/**
 * FXML Controller class
 *
 * @author lainh_000
 */
public class FX_HoaDonController implements Initializable {

    
    //khai báo control
    @FXML TextField txt_hotenkh;
    @FXML TextField txt_hotennv;
    @FXML TextField txt_tongtien;
    @FXML Button btn_add;
    @FXML Button btn_edit;
    @FXML Button btn_search;
    @FXML Button btn_del;
    @FXML TableView table_hd;
    @FXML TableColumn col_mahd;
    @FXML TableColumn col_tenkh;
    @FXML TableColumn col_tennv;
    @FXML TableColumn col_ngaylap;
    @FXML TableColumn col_tongtien;
    @FXML TableView table_sach;
    @FXML TableColumn col_tensach;
    @FXML TableColumn col_soluong;
    @FXML TableColumn col_dongia;
    
    SQLConnection connect = new SQLConnection();
    ObservableList<HOADON> data_hd = FXCollections.observableArrayList();
    ObservableList<SACH> data_sach = FXCollections.observableArrayList();
    public DatePicker dtpk = new DatePicker();
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        KhoiTao();
        dtpk.setDateFormat(new SimpleDateFormat("MM-dd-yyyy"));
        configureTextField();
    }    
    
    private void KhoiTao()
    {
        assert txt_hotenkh != null;
        assert txt_hotennv != null;
        assert txt_tongtien != null;
        assert btn_add != null;
        assert btn_edit != null;
        assert btn_del != null;
        assert btn_search != null;
        assert table_hd != null;
        assert table_sach != null;        
    }
    
    //chỉ có nhân viên có chức vụ quản lý mới đươc xóa hóa đơn
    private void configureButton()
    {
       
    }
    
    //TextField tên nhân viên và tổng tiền ko được nhập
    private void configureTextField()
    {
        txt_hotennv.setEditable(false);
        txt_tongtien.setEditable(false);
    }
    
    
    private void configureTableHD()
    {
        col_mahd.setCellFactory(new PropertyValueFactory<HOADON, String>("mahd"));
        col_tenkh.setCellValueFactory(new PropertyValueFactory<HOADON, String>("khachhang"));
        col_tennv.setCellValueFactory(new PropertyValueFactory<HOADON, String>("hoten"));
        col_ngaylap.setCellValueFactory(new PropertyValueFactory<HOADON, String>("nhanvien"));
        col_tongtien.setCellValueFactory(new PropertyValueFactory<HOADON, String>("tongtien"));
        
        table_hd.setItems(data_hd);
        table_hd.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        table_hd.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() 
        { 
            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) 
            { 
                HOADON hd = (HOADON)newValue;
                txt_tongtien.setText(String.valueOf(hd.getTongtien()));
            } 
        });
    }
    
    
    public void btn_addClick(ActionEvent event)
    {
        try {

            connect.getConnect();
            
//            String query = "INSERT INTO KHACHHANG (hoten,diachi,sodt) "
//                    + "VALUES('"+hoten+"','"+diachi+"','"+sodt+"')";
//            int rs = connect.executeUpdate(query);
            JOptionPane.showMessageDialog(null,"Thêm thành công");
        } catch (Exception ex) {
            String stb="Thêm dữ liệu không thành công.";
            JOptionPane.showMessageDialog(null,stb);       
        }
    }
    
    public void btn_editClick(ActionEvent event)
    {
         try {
            connect.getConnect();

//            String query = "UPDATE KHACHHANG "
//                    + "SET hoten = N'"+hoten+"',diachi = N'"+diachi+"', sodt ='"+sodt+"' WHERE makh ="+kh.getMakh()+";";
//            int rs = connect.executeUpdate(query);
            JOptionPane.showMessageDialog(null,"Cập nhật thành công");
        } catch (Exception ex) {
            String stb="Cập nhập dữ liệu không thành công.";
            JOptionPane.showMessageDialog(null,stb);       
        }
    }
    
    public void btn_delClick(ActionEvent event)
    {
         try {
            connect.getConnect();

//            String query = "DELETE FROM KHACHHANG WHERE makh = "+kh.getMakh()+";";
//            int result = connect.executeUpdate(query);
            JOptionPane.showMessageDialog(null,"Xóa thành công");
        } catch (Exception ex) {
            String stb="Xóa dữ liệu không thành công.";
            JOptionPane.showMessageDialog(null,stb);       
        }
    }
    
    public void btn_searchClick(ActionEvent event)
    {
        
    }
}
